package com.bdk.middle.tenant.web.orchestration;


import com.bdk.middle.common.result.ResultDO;
import com.bdk.middle.common.result.web.ObjectResponse;
import com.bdk.middle.orchestration.api.client.ModuleClientApi;
import com.bdk.middle.orchestration.api.domain.FunctionModuleDO;
import com.bdk.middle.orchestration.api.query.FunctionModuleQueryDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Reagan
 * @since 2021-03-12
 */
@RestController
@RequestMapping("/function-module")
public class FunctionModuleController {

    @Autowired
    private ModuleClientImpl moduleClientImpl;

    @FeignClient(value= "orchestration-service", contextId="module", path = "/app")
    interface ModuleClientImpl extends ModuleClientApi {

    }


    @RequestMapping(value="/module_list.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse list(@RequestBody FunctionModuleDO domain) {

        ObjectResponse<List<FunctionModuleDO>> response = new ObjectResponse();

        ResultDO result = moduleClientImpl.getAvailableModule();

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((List<FunctionModuleDO>) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }


    @RequestMapping(value="/detail.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse detail(@RequestBody FunctionModuleDO domain) {

        ObjectResponse<Map<Object, Object>> response = new ObjectResponse();

        ResultDO result = moduleClientImpl.get(domain.getId());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((Map<Object, Object>) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }

    @RequestMapping(value="/list.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse list(@RequestBody FunctionModuleQueryDO domain) {

        ObjectResponse<Map<Object, Object>> response = new ObjectResponse();

        ResultDO result = moduleClientImpl.find(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((Map<Object, Object>) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }
}

